import { observable, computed, action, toJS } from 'mobx'
//引入自己封装的jwt工具类
import JWT from '@/utils/jwtutil'
//引入全局变量
import CONSTS from '@/inits/consts'
//设置默认正确用户名
const LOGINDATA = CONSTS.LOGINDATA
class LoginStore {
  @observable username: string = ""
  @observable password: string = ""
  @observable checked: boolean = false
  @observable activeUser: string = ""
  //修改登录用户信息
  @action setActiveUser(username?: string) {
    this.activeUser = username || "游客"
  }   
  //写入修改输入框用户信息函数
  @action setUser(params: any) {
    this.username = params.username
    this.password = params.password
    this.checked = params.checked
  }
  //初始化用户数据函数
  @action initUser() {
    const infos = JWT.verify(localStorage.getItem('dbtoken'), LOGINDATA.KEY)//如果没有localStorage的值,解密也是失败
    let init: any = {}
    //当解密出来后的基础密码值为true,将解密后的值绑定到this.state中渲染(这里将密码一起记住渲染了,方便测试,如果不用直接删除即可)
    if (infos.isLogin) {
      infos.decode.sign.checked ? this.setUser({ username: infos.decode.sign.username, password: infos.decode.sign.password, checked: infos.decode.sign.checked }) : "";
      this.setActiveUser(infos.decode.sign.username)
    }
   //返回初始化数据
   this.checked ? init = { remember: this.checked, username: this.username, password: this.password } : init = {}
    return init
  }
}
//在这里实例化后再导出,方便后面使用
let store = new LoginStore()
export default store

